﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EraNet.Core;
using EraNet.Core.Caching;
using EraNet.Core.Data;
using EraNet.Core.Events;
using EraNet.Core.Model.SysManage;
using EraNet.Common;

namespace EraNet.Services.SysManage
{
    public partial class DictionaryService:IDictionaryService
    {
        #region Constants
        private const string SYSDICTIONARY_BY_ID_KEY = "Era.SysDictionary.id-{0}";
        private const string SYSDICTIONARY_PATTERN_KEY = "Era.SysDictionary.";
        private const string SYSDICTIONARY_ALL_KEY = "Era.SysDictionary.Mark8276n36";
        #endregion

        #region Fields
        private readonly IRepository<SysDictionary> _sysdictionaryRepository;
        //private readonly IRepository<DictionaryType> _dictionaryTypeRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        #endregion
        
        #region Ctor
        public DictionaryService(IRepository<SysDictionary> sysdictionaryRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            this._sysdictionaryRepository = sysdictionaryRepository;
            this._cacheManager = cacheManager;
            this._eventPublisher = eventPublisher;
        }
        #endregion

        #region Methods
        ///// <summary>
        ///// 插入字典类型表
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns></returns>
        //public int InsertDictionaryType(DictionaryType model)
        //{
        //    if (model == null)
        //        throw new Exception("Null DictionaryType Model");
        //    //验证是否已经存在
        //    var query = _dictionaryTypeRepository.Table.Where(c => c.KeyValue == model.KeyValue && c.DtType == model.DtType);
        //    if (query.FirstOrDefault() != null) return 2;
        //    int rs = _dictionaryTypeRepository.Insert(model) > 0 ? 1 : 0;
        //    return rs;
        //}

        ///// <summary>
        ///// 根据ID获取字典类型信息
        ///// </summary>
        ///// <param name="dictionarytypeid"></param>
        ///// <returns></returns>
        //public DictionaryType GetDictionaryType(int dictionarytypeid)
        //{
        //    if (dictionarytypeid <= 0) return null;
        //    var m = _dictionaryTypeRepository.GetById(dictionarytypeid);
        //    return m;
        //}

        ///// <summary>
        ///// 根据ID删除数据
        ///// </summary>
        ///// <param name="dictionarytypeid"></param>
        ///// <returns></returns>
        //public bool DeleteDictionaryType(int dictionarytypeid)
        //{
        //    if (dictionarytypeid <= 0) return false;
        //    var m = _dictionaryTypeRepository.GetById(dictionarytypeid);
        //    bool  rs=_dictionaryTypeRepository.Delete(m)>0?true:false;
        //    return rs;
        //}

        /// <summary>
        /// 插入新的字典
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int InsertDictionary(SysDictionary model)
        {
            if (model == null)
                throw new ExtException("Null SysDictionary Model");
            //验证是否已经存在
            if (GetAllDictionary().Where(c => c.KeyValue == model.KeyValue && c.TypeKey == model.TypeKey).Count() >0) return 2;
            int rs = _sysdictionaryRepository.Insert(model) > 0 ? 1 : 0;
            
            _cacheManager.Remove(SYSDICTIONARY_ALL_KEY);

            //event notification
            _eventPublisher.EntityInserted(model);
            return rs;
        }

        /// <summary>
        /// 插入新的字典数据
        /// </summary>
        /// <param name="model">字典类型</param>
        /// <param name="IsAstrict">是否受限数据</param>
        /// <returns></returns>
        public int InsertDictionary(IList<SysDictionary> model, bool IsAstrict)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="dictionaryId"></param>
        /// <returns></returns>
        public int DeleteDictionary(int dictionaryId)
        {
            if (dictionaryId <=0)
                throw new ExtException("Null SysDictionary Model");
            var m = GetDictionaryById(dictionaryId);
            
            int rs = _sysdictionaryRepository.Delete(m);

            if(rs>0) _cacheManager.Remove(SYSDICTIONARY_ALL_KEY);

            //event notification
            _eventPublisher.EntityDeleted(m);
            return rs;
        }

        public SysDictionary GetDictionaryById(int dictionaryId)
        {
            if (dictionaryId<=0)
                return null;

            var m = GetAllDictionary().Where(c => c.Id == dictionaryId).FirstOrDefault();
            
            return m;
        }

        public IList<SysDictionary> GetDictionaryByKey(string Keys)
        {
            if (string.IsNullOrEmpty(Keys))
                return null;

            var m = GetAllDictionary().Where(c => c.KeyStr == Keys).ToList();

            return m;
        }

        public IList<SysDictionary> GetDictionaryByTypeKey(string TypeKey)
        {
            if (string.IsNullOrEmpty(TypeKey))
                return null;

            var m = GetAllDictionary().Where(c => c.TypeKey == TypeKey).ToList();

            return m;
        }

        public IList<SysDictionary> GetAllDictionary()
        {
            return _cacheManager.Get(SYSDICTIONARY_ALL_KEY, () =>
            {
                var p = _sysdictionaryRepository.Table.ToList();
                return p;
            });
        }

        public IPagedList<SysDictionary> GetAllDictionary(string Keys, int pageSize, int pageIndex, bool isShowHide)
        {
            var query = GetAllDictionary();
            if (!isShowHide) query = query.Where(c => c.Status == 1).ToList();
            query = query.Where(c => c.KeyStr == Keys).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var sysdictionary = new PagedList<SysDictionary>(query, pageIndex, pageSize);
            return sysdictionary;
        }
        #endregion

    }
}
